/*
 * StartClientDialog.java
 *
 * Created on 2009-maj-15, 09:22:06
 */
package conf2.ui;

import conf2.text.Misc;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.KeyStroke;
import javax.swing.text.JTextComponent;

/**
 *
 * @author Administrator
 */
public class StartClientDialog extends javax.swing.JDialog
{

    public static class ReturnValue
    {

        public final String name;
        public final String host;
        public final int port;

        public ReturnValue(String name, String host, int port)
        {
            this.name = name;
            this.host = host;
            this.port = port;
        }
    }
    private ReturnValue returnValue = null;

    /** Creates new form StartClientDialog */
    private StartClientDialog(java.awt.Frame parent, String defaultName, String defaultHost, int defaultPort)
    {
        super(parent, true);

        initComponents();

        FrameUtil.bindKey(this, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), new AbstractAction()
        {

            public void actionPerformed(ActionEvent e)
            {
                cancelButtonActionPerformed(null);
            }
        });

        FocusListener focusListener = new FocusAdapter()
        {

            @Override
            public void focusGained(FocusEvent e)
            {
                JTextComponent source = (JTextComponent) e.getSource();
                source.setSelectionStart(0);
                source.setSelectionEnd(source.getText().length());
            }
        };

        InputValidator.bind(nameField, okButton, InputValidator.NAME_RESTRICTION);
        nameField.setText(defaultName);
        nameField.addFocusListener(focusListener);
        nameField.requestFocusInWindow();

        InputValidator.bind(ipField, okButton, InputValidator.IP_RESTRICTION);
        ipField.setText(defaultHost);
        ipField.addFocusListener(focusListener);

        InputValidator.bind(portField, okButton, InputValidator.PORT_RESTRICTION);
        portField.addFocusListener(focusListener);
        portField.setText(String.valueOf(defaultPort));

        getRootPane().setDefaultButton(okButton);

        setLocationRelativeTo(parent);
    }

    public static ReturnValue show(Frame parent, String defaultName, String defaultHost, int defaultPort)
    {
        StartClientDialog dlg = new StartClientDialog(parent, defaultName, defaultHost, defaultPort);
        dlg.setVisible(true);
        return dlg.returnValue;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        titleLabel = new javax.swing.JLabel();
        okButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        nameField = new javax.swing.JTextField();
        portField = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        ipField = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle(Misc.CONNECT_DLG_TITLE);

        titleLabel.setFont(FontSet.getDraftCaption());
        titleLabel.setText(Misc.CONNECT_DLG_CAPTION);

        okButton.setText(Misc.OK_BUTTON_LABEL);
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });

        cancelButton.setText(Misc.CANCEL_BUTTON_LABEL);
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        jPanel1.setLayout(new java.awt.GridBagLayout());
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 0.8;
        gridBagConstraints.insets = new java.awt.Insets(4, 15, 4, 0);
        jPanel1.add(nameField, gridBagConstraints);

        portField.setColumns(6);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 0.8;
        gridBagConstraints.insets = new java.awt.Insets(4, 15, 4, 0);
        jPanel1.add(portField, gridBagConstraints);

        jLabel2.setText(Misc.HOST_LABEL);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 0.2;
        jPanel1.add(jLabel2, gridBagConstraints);

        jLabel3.setText(Misc.PORT_NUMBER_LABEL);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 0.2;
        jPanel1.add(jLabel3, gridBagConstraints);

        jLabel1.setText(Misc.YOUR_NAME_LABEL);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 0.2;
        jPanel1.add(jLabel1, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 0.8;
        gridBagConstraints.insets = new java.awt.Insets(4, 15, 4, 0);
        jPanel1.add(ipField, gridBagConstraints);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(cancelButton, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(okButton, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 332, Short.MAX_VALUE)
                    .addComponent(titleLabel))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(titleLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 25, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(okButton)
                    .addComponent(cancelButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        dispose();
    }//GEN-LAST:event_cancelButtonActionPerformed

    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
        returnValue = new ReturnValue(nameField.getText().trim(), ipField.getText().trim(), Integer.valueOf(portField.getText()));
        setVisible(false);
    }//GEN-LAST:event_okButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JTextField ipField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField nameField;
    private javax.swing.JButton okButton;
    private javax.swing.JTextField portField;
    private javax.swing.JLabel titleLabel;
    // End of variables declaration//GEN-END:variables
}
